package codeanticode.vision;

import processing.core.PApplet;
import java.io.File;
import com.googlecode.javacv.OpenCVFrameGrabber;

public class MovieGrabber extends BaseGrabber {
  public MovieGrabber(PApplet parent, String filename) {
    super(parent);   
    
    File file;
    
    // first check to see if this can be read locally from a file.
    try {
      try {
        // first try a local file using the dataPath. usually this will
        // work ok, but sometimes the dataPath is inside a jar file,
        // which is less fun, so this will crap out.
        file = new File(parent.dataPath(filename));
        if (file.exists()) {
          //grabber = new FFmpegFrameGrabber(file);
          grabber = new OpenCVFrameGrabber(file);
        }
      } catch (Exception e) {
      } // ignored        
    
      // read from a file just hanging out in the local folder.
      // this might happen when the video library is used with some
      // other application, or the person enters a full path name
      if (grabber == null) {
        try {
          file = new File(filename);
          if (file.exists()) {
            //grabber = new FFmpegFrameGrabber(file);
            grabber = new OpenCVFrameGrabber(file);
          }
        } catch (Exception e) {          
          e.printStackTrace();
        }
      }    
      
      // Network read...      
      if (grabber == null) {
        try {
          // ??          
        } catch (Exception e) {
          e.printStackTrace();
        }      
      }
      
    } catch (SecurityException se) {
      // online, whups. catch the security exception out here rather than
      // doing it three times (or whatever) for each of the cases above.
    }

    if (grabber == null) {
      parent.die("Could not load movie file " + filename, null);
    }      

    firstFrame = true;
  } 
}
